package display
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class Grids extends Sprite
	{
		private var _colNum:int;
		private var _rowNum:int;
		private var _gridWidth:int;
		
		private var _blocks:Vector.< Vector.<Boolean> >;
		
		public function Grids(rowNum:int, colNum:int, gridWidth:int = 50)
		{
			super();
			_colNum = colNum;
			_rowNum = rowNum;
			_gridWidth = gridWidth;
			_blocks = new Vector.< Vector.<Boolean> >(rowNum);
			for (var i:int = 0; i < rowNum; i++) {
				_blocks[i] = new Vector.<Boolean>(colNum);
			}
			addEventListener(MouseEvent.RIGHT_MOUSE_DOWN, onRightMouseDown);
			var hit:Sprite = new Sprite();
			hit.alpha = 0;
			hit.graphics.beginFill(0x000000);
			hit.graphics.drawRect(0, 0, colNum*gridWidth, rowNum*gridWidth);
			addChild(hit);
			rendering();
		}
		
		private function onRightMouseDown(e:MouseEvent):void {
			var x:int = e.localX / _gridWidth;
			var y:int = e.localY / _gridWidth;
			trace("click x:" + x + " y:" + y);
			_blocks[y][x] = !_blocks[y][x];
			rendering();
		}
		
		public function rendering():void {
			var g:Graphics = this.graphics;
			var x:int = _gridWidth * _colNum;
			var y:int;
			var i:int;var j:int;
			g.clear();
			g.lineStyle(1);
			for (i = 1; i < _rowNum; i++) {
				y = _gridWidth * i;
				g.moveTo(0, y);
				g.lineTo(x, y);
			}
			y = _gridWidth * _rowNum;
			for (j = 1; j < _colNum; j++) {
				x = _gridWidth * j;
				g.moveTo(x, 0);
				g.lineTo(x, y);
			}
			g.beginFill(0xFF0000);
			for (i = 0; i < _rowNum; i++) {
				for (j = 0; j < _colNum; j++) {
					if (_blocks[i][j]) {
						g.drawRect(j*_gridWidth, i*_gridWidth, _gridWidth, _gridWidth); 
					}
				}
			}
			g.endFill();
		}

	}
}